<?php

namespace bee;
class user extends DAO
{	public $id;
	public $login;
	public $password;
	public $status;
	public $description;
	public $last_login_date;
	public $org_id;
	public $view_rights;
	public $bad_logins;
	public $number_of_visits;

	/*
	public function INSERT($params = DAO_POST_ALL)
	{  parent::INSERT($params);
	}
	*/

	function UPDATE()
	{	$sqt = "UPDATE $this->__table SET
					login 						= :login, ";
	  if($this->password != "" && $this->password != sha1(""))
	  {  $sqt .= "password = :password, ";
	  }
		$sqt .= " status 			  = :status,
					description 			= :description,
					last_login_date 	= :last_login_date,
					org_id 						= :org_id,
					view_rights 			= :view_rights,
					bad_logins 				= :bad_logins,
					number_of_visits 	= :number_of_visits
					WHERE id 			 		=	:id";
		$stmt = $this->__conn->prepare($sqt);
		//number_of_visits 	= :number_of_visits
		//$this->__login = iconv("UTF-8", "Windows-1250", $this->login);
    //$this->__description= iconv("UTF-8", "Windows-1250", $this->description);
		$__login =  $this->login;
		//$this->__description= iconv('UTF-8', 'Windows-1250', $this->description);
		$__description =  $this->description;


		if($this->password != "" && $this->password != sha1(""))
    {	$__password = iconv("UTF-8", "Windows-1250", $this->password);
			$stmt->bindParam(":password", $__password, \PDO::PARAM_STR);
		}
		$stmt->bindParam(":login", $__login, \PDO::PARAM_STR);
		$stmt->bindParam(":status", $this->status, \PDO::PARAM_INT);

		$stmt->bindParam(":description", $__description, \PDO::PARAM_STR);
		$stmt->bindParam(":last_login_date", $this->last_login_date);
		$stmt->bindParam(":org_id", $this->org_id, \PDO::PARAM_INT);
		$stmt->bindParam(":view_rights", $this->view_rights, \PDO::PARAM_INT);
		$stmt->bindParam(":bad_logins", $this->bad_logins, \PDO::PARAM_INT);
		$stmt->bindParam(":id", $this->id, \PDO::PARAM_INT);
//print_r($this);
//echo $sqt;
		$stmt->bindParam(":number_of_visits", $this->number_of_visits, \PDO::PARAM_INT);
		if($stmt->execute())
		{	return 1;
		}
		return 0;
	}
	/**
	 *
	 * @param int $login
	 * @return bee\user
	 */
	public static function getByLogin($login)
	{
	  $user = new user();
	  if($login)
	  {
	    $user->login = $login;
	    $rs = new rowSet($user);
	    if($rs->getRow($user))
	    {
	    	return $user;
	    }
	   // die("*** $login ***" . print_r($user, true) . "****");
	    return new user();
	  }

	  return $user;
	}


}
